class MyPage extends BasePopupEdit {
    constructor(dataId, pageStatus) {
        super(dataId, pageStatus);
        this._rows = 0;
        this.arrStyleNo = parent.$("#SuitStyleNo").val().split("/");
    }
    OnPageLoad() {
        setTimeout(() => {
            this.BuildDataTable();
            $('#loading').hide();
        }, 300);
    }
    // 得到检索条件
    GetFilter() {
        let data = new Object();
        return $.toJSON(data);
    }
    BuildDataTable() {
        let content = '';
        // step1: 清空尺码设置的表头以及表格内容
        $("#tabHeader01 tbody").empty();
        $("#tabData01 tbody").empty();

        // step2: 设置列宽度
        $("#tabHeader01 colgroup").empty();
        $("#tabData01 colgroup").empty();
        content += '<col style="width: 36px;" />';
        for (let i = 0; i < this.arrStyleNo.length - 1; i++) {
            content += '<col style="width: 240px;" />';
        }
        $("#tabData01 colgroup").append(content);
        $("#tabHeader01 colgroup").append(content);

        // step3: 设置表头
        content = '';
        $("#tabHeader01 thead tr").empty();
        content += '<th style="text-align:center;">删除</th>';
        if (this.arrStyleNo.length > 0) {
            for (let i = 0; i < this.arrStyleNo.length; i++) {
                content += '<th class="editable__column">' + '尺码（' + this.arrStyleNo[i] + '）</th>';
            }
        }
        $("#tabHeader01 thead tr").append(content);
    }
    GetLinesData() {
        let jsonUpdate = [];
        let itemResult = {};
        // 得到修改过数据
        $('#tabData01 tbody').find("tr").each(function () {
            if (this.arrStyleNo.length > 0) {
                for (let i = 1; i < $(this).find("td").length; i++) {
                    let item = {};
                    item['colourId'] = parseInt($("#ProductColourItemList").val());
                    item['sizeName'] = $(this).find("td").eq(i).text();
                    item['createdBy'] = parseInt(parent.$("#UserId").val());
                    item['updatedBy'] = parseInt(parent.$("#UserId").val());
                }
            }
        });
        itemResult['ItemList'] = jsonUpdate;
        return itemResult;
    }
    // 保存
    DoSave() {
        if ($("#ProductColourItemList").val() === "-1") {
            ShowErrorMessagePanel("请选择成品色。");
            return false;
        }
        if (this.GetLinesData().UpdateList.length > 0) {
            super.DoAjax(this.GetLinesData(), "/trade/production_instruction/lines/size/update.html", (data, _this) => {
                parent.arrChoice.push(_this.GetLinesData().SuitSizeName);
                parent.choice_success = true;
                closePopupWnd();
            });
        }
    }
    // 添加新行以及新行的操作方式
    InsertNewLines() {
        let randomId = Math.round(new Date().getTime() / 1000);
        let _row = "";
        if (this.arrStyleNo.length > 0) {
            for (let i = 0; i < this.arrStyleNo.length; i++) {
                if (i === (this.arrStyleNo.length - 1)) {
                    _row += '<td class="edited-cell" style="border-right:none;"></td>';
                } else {
                    _row += '<td class="edited-cell"></td>';
                }
            }
        }
        let $newLine = $('<tr class="newLine" _mash_id="0">' +
                '    <td class="column__action"><span title="删除" class="trash" onclick="_Instance.RemoveNewLine($(this))"><i class="fa-all fa-trash-o fa-1"></i></span></td>' +
                _row +
                '</tr>');
        $('#tabData01 tbody').prepend($newLine);   // 加在前面
        this._rows = this._rows + 1;

        setOneTableRowColor("tabData01");
        this.AutoAdjustTableHeightForLines();  // 自适应高度设置

        let id = "cell__field";
        let name = "_cell__field";
        $newLine.find("td.edited-cell").click(function () {
            OnCellClickEvent($(this), id, name, 0);
        });
    }
    RemoveNewLine($self) {
        showModal(350, 140, '删除确认', '', null, null, 1, '请问，确定要删除当前行吗？', () => {
            $self.parent().parent().remove();
            this.rows = this.rows - 1;
            // 自适应高度设置
            this.AutoAdjustTableHeightForLines();
        });
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _top = $("#tabHeader01").offset().top + 25;
        let _rc = Math.floor((h - _top - 25) / 25);
        let _dh = _rc * 25;
        let _tah = this._rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#divContent").css({"max-height": _dh, height: _dh});
    }
}

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#form_data").data("changed", false);   // 初始化form数据提交状态
    _Instance = new MyPage(0, 1);
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}
